package org.answer.controller;


import org.answer.constant.MessageConstant;
import org.answer.entity.Result;
import org.answer.service.MedicalUploadService;
import org.answer.service.MemberService;
import org.answer.utils.POIUtils;
import org.answer.utils.QiniuUtils;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@RequestMapping("/medical")
@RestController
public class MedicalUploadController {


    @Reference
    private MedicalUploadService medicalUploadService;



    @RequestMapping("/upload")
    private Result upload(@RequestParam("imgFile") MultipartFile imgFile,
                          @RequestParam("fileNumber") String fileNumber,
                          @RequestParam("name") String name,
                          @RequestParam("id") Integer id){
        try {
            List<String[]> strings = POIUtils.readExcel(imgFile);

            String[] row_4 = strings.get(5);
            String name1 = row_4[2];   //姓名
            String[] row_2 = strings.get(2);
            String number = row_2[4];  //档案号
            String[] row_3 = strings.get(3);
            String healthDate  = row_3[6];  //日期

            //将日期/ 转换为 - 用于构建用户病历名称
            String date = healthDate.replace("/", "-");

            String originalFilename = imgFile.getOriginalFilename();  //获取文件名称

            //获取 . 在文件种所在位置
            int index = originalFilename.lastIndexOf(".");
            //对. 经行切割 剩下文件类型名
            String endName = originalFilename.substring(index);

            //组成新的文件名，  //命名格式  杨森2020-11-11-10086.xlsx
            String fileName = name1 + date + "-" +number + endName;

            //"http://qjm4nsfv5.hn-bkt.clouddn.com/"

            if(name.equals(name1)&&number!=""){
                QiniuUtils.upload2Qiniu(imgFile.getBytes(),fileName);
                medicalUploadService.add(number,"http://qjm4nsfv5.hn-bkt.clouddn.com/"+fileName,id);
                return new Result(true, MessageConstant.HEALTH_UPLOAD_SUCCESS);
            }else {
                throw new RuntimeException("体检报告表种编号和用户编号不一致");
            }
        }catch (RuntimeException e){
            e.printStackTrace();
            return new Result(false, MessageConstant.FILENAME_INCONFORMITY);
        }catch (Exception e){
            return new Result(false, MessageConstant.HEALTH_UPLOAD_FAIL);
        }



    }

    /**
     * 下载
     * @param id
     * @return
     */

    @RequestMapping("/downloadHealthFrom")
    public Result downloadHealthFrom(Integer id){
        try {
            List<String> list = medicalUploadService.downloadHealthFrom(id);
            if (list.size()==0){
                //表示没有体检报告
                throw new RuntimeException("该用户没有体检报告");
            }
            return new Result(true, MessageConstant.DOWNLOAD_DELETE_SUCCESS,list);
        } catch (RuntimeException e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.NOTFINDHEALTH);
        }catch (Exception e){
            return new Result(false, MessageConstant.DOWNLOAD_DELETE_FAIL);
        }
    }


    @RequestMapping("/delete")
    public Result delete(Integer id){
        try {
            List<String> list = medicalUploadService.downloadHealthFrom(id);
            if(list.size()>0){
                for (String s : list) {
                    String[] split = s.split("com/");
                    QiniuUtils.deleteFileFromQiniu(split[1]);
                }
            }
            medicalUploadService.delete(id);

            return new Result(true, MessageConstant.HEALTH_DELETE_SUCCESS);
        } catch (RuntimeException e){
            e.printStackTrace();
            return new Result(false, MessageConstant.NOTFINDHEALTH);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.HEALTH_DELETE_FAIL);
        }
    }




}
